package dao.wechatlab.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import dao.BaseDao;
import dao.wechatlab.LabelSystemDao;
import pojo.jane.AuthorityBean;
import pojo.jane.KeyWordsBean;
import pojo.jane.OperationBean;
import pojo.jane.StudentBean;

@Repository
@SuppressWarnings("unchecked")
public class LabelSystemDaoImpl extends BaseDao implements LabelSystemDao {

	// 插入新注册的账户
	@Override
	public void insertStudent(StudentBean st) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().saveOrUpdate(st);
	}

	// 获取用户
	@Override
	public List<StudentBean> selectStudent(String userid) {
		// TODO Auto-generated method stub
		List<StudentBean> student = new ArrayList<StudentBean>();
		DetachedCriteria dc = DetachedCriteria.forClass(StudentBean.class);
		dc.add(Restrictions.eq("userid", userid));
		student = (List<StudentBean>) this
				.getHibernateTemplate().findByCriteria(dc);
		return student;
	}
	
	
	@Override
	public List<AuthorityBean> selectAuthid(int authgroup) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = DetachedCriteria.forClass(AuthorityBean.class);
		dc.add(Restrictions.eq("authgroup", authgroup));
		
		return (List<AuthorityBean>)this.getHibernateTemplate().findByCriteria(dc);
	}


	// 获得用户ip
	public String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	@Override
	public List<OperationBean> selectOperation(int authid) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = DetachedCriteria.forClass(OperationBean.class);
		dc.add(Restrictions.eq("id", authid));
		
		return (List<OperationBean>)this.getHibernateTemplate().findByCriteria(dc);
	}

	@Override
	public void addKeyWords(KeyWordsBean kw) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().saveOrUpdate(kw);
	}

	@Override
	public boolean updateUser(StudentBean st) {
		// TODO Auto-generated method stub
		List<StudentBean> stlist = selectStudent(st.getUserid());
		if(stlist.size() == 1){
			StudentBean oldst = stlist.get(0);
			oldst.setEmail(st.getEmail());
			oldst.setIdentity(st.getIdentity());
			oldst.setName(st.getName());
			oldst.setPhone(st.getPhone());
			oldst.setupper(st.getupper());
			oldst.setlower(st.getlower());
			this.getHibernateTemplate().update(oldst);
			
				
		}
		return false;
	}

}
